﻿using AutoMapper;
using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Write.API.Application.Commands.IssueFeedbackCommand;
using MediatR;

namespace D.Medicalcare.Write.API.Application.Handlers.IssueFeedbackHandler
{
    /// <summary>
    /// 添加反馈
    /// </summary>
    public class AddIssueFeedbackHandler : IRequestHandler<AddIssueFeedbackCommand, APIResult<int>>
    {
        private readonly IBaseRepository<IssueFeedback> baseRepository;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseRepository">存储</param>
        /// <param name="mapper">映射</param>
        public AddIssueFeedbackHandler(IBaseRepository<IssueFeedback> baseRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">参数</param>
        /// <returns></returns>
        public async Task<APIResult<int>> Handle(AddIssueFeedbackCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<int> result = new APIResult<int>();
                var entity=mapper.Map<IssueFeedback>(request);
                var dto =baseRepository.AddAsync(entity).Result;
                if (dto>0)
                {
                    result.Code = APIEnum.Success;
                    result.Msg = "添加成功";
                    result.Data = dto;
                }
                else
                {
                    result.Code = APIEnum.Failed;
                    result.Msg = "添加失败";
                    return await Task.FromResult(result);
                }
                return await Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
